<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController {
    //普通用户列表
    public function user_list(){
        $map = '';
        $is_user_type = I('is_user_type');
        if($is_user_type  != ''){
            $map['user_type'] = $is_user_type;
        }

        $is_disabled = I('is_disabled');
        if($is_disabled != ''){
            $map['disabled'] = $is_disabled;
        }

        $search_content = I('search_content');
        if($search_content  != ''){
            $where['username'] = array('like',"%$search_content%");
            $where['phone'] = array('like',"%$search_content%");
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }

        $row = 10;
        $p = $_GET['p'] ? $_GET['p'] : 1;
        $count = M('user')->where($map)->count();
        $Page = new \Think\Page($count,$row);
        $show = $Page->show();
        $list = M('user')->where($map)->page($p,$row)->order('id desc')->select();
        $this->assign('p',$p);
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->assign('is_user_type',$is_user_type);
        $this->assign('is_disabled',$is_disabled);
        $this->assign('search_content',$search_content);
        $this->display();
    }

    //普通用户的添加
    public function user_add(){
        if(IS_POST){
            //判断用户名和手机号是否重复
            if(M('user')->where(array('username'=>I('username')))->getField('id')){
                $this->error('该用户名已存在');
            }

            if(M('user')->where(array('phone'=>I('phone')))->getField('id')){
                $this->error('该手机号已存在');
            }

            $picture = uploadPicture();
            if($picture) {
                $data['head_picture'] = $picture;
            }
            $data['username'] = I('username');
            $data['phone'] = I('phone');
            $data['password'] = md5(I('password'));
            $data['sex'] = I('sex');
            $data['fixed_telephone'] = I('fixed_telephone');
            $data['email'] = I('email');
            $data['qq'] = I('qq');
            $data['wechat'] = I('wechat');
            $data['add_time'] = date('Y-m-d H:i:s');
            if(false === M('user')->add($data)){
                $this->error('添加失败');
            }else{
                $this->success('添加成功',U('user_list'));
            }
        }

        //获取筛选和分页参数，分配变量
        $this->display();
    }

    //普通用户的编辑
    public function user_edit(){
        $info = M('user')->where('id='.I('id'))->find();
        if(IS_POST){
            $id = I('id');
            //如果能接收到文件，执行上传
            if($_FILES){
                $picture = uploadPicture();
                if($picture) {
                    $data['head_picture'] = $picture;
                }
            }
            $data['username'] = I('username');
            $data['phone'] = I('phone');
            $data['sex'] = I('sex');
            $data['disabled'] = I('disabled');
            $data['fixed_telephone'] = I('fixed_telephone');
            $data['email'] = I('email');
            $data['qq'] = I('qq');
            $data['wechat'] = I('wechat');
            if(false === M('user')->where('id='.$id)->save($data)){
                $this->error('修改失败');
            }else{
                if($_FILES){
                    unlink(ltrim($info['head_picture'],'/'));//删除已经上传的图片
                }
                $this->success('修改成功',U('user_list',array('p'=>I('p'),'is_user_type'=>I('is_user_type'),'is_disabled'=>I('is_disabled'),'search_content'=>I('search_content'))));
            }
        }

        //获取筛选和分页参数，分配变量
        $this->assign('p',I('p'));
        $this->assign('is_user_type',I('is_user_type'));
        $this->assign('is_disabled',I('is_disabled'));
        $this->assign('search_content',I('search_content'));
        $this->assign('info',$info);
        $this->display();
    }

    //成为电工
    public function become_electrician(){
        if(IS_POST){
            $id = I('id');
            //先判断身份证号是否重复
            $data['id_number'] = I('id_number');
            if(M('electrician')->where(array('id_number'=>$data['id_number']))->getField('id')){
                $this->error('该身份证号已存在');
            }
            //上传文件
            $picture = upload();
            if($picture) {
                $data['identity_card_front'] = $picture['identity_card_front'];
                $data['identity_card_reverse'] = $picture['identity_card_reverse'];
                $data['electrician_license_front'] = $picture['electrician_license_front'];
                $data['electrician_license_reverse'] = $picture['electrician_license_reverse'];
            }
            $data['electrician_id'] = I('id');
            $data['real_name'] = I('real_name');
            $data['check_status'] = 1;
            $data['add_time'] = date('Y-m-d H:i:s');
            $data['check_time'] = date('Y-m-d H:i:s');
            $data['province'] = I('province');
            $data['city'] = I('city');
            $data['county'] = I('county');
            $data['town'] = I('town');
            //开启事务，先修改user表，再添加electrician表
            M()->execute('begin');
            if(false === M('user')->where('id='.$id)->save(array('vip'=>1,'user_type'=>2))){
                M()->execute('rollback');
                $this->error('操作失败');
            }
            $addResult = M('electrician')->add($data);
            if(!$addResult){
                M()->execute('rollback');
                $this->error('操作失败');
            }

            M()->execute('commit');
            $this->success('操作成功',U('user_list',array('p'=>I('p'),'is_user_type'=>I('is_user_type'),'is_disabled'=>I('is_disabled'),'search_content'=>I('search_content'))));
        }

        //获取所有省份
        $province_list = M('area')->where(array('parentId' => 0))->select();

        //获取筛选和分页参数，分配变量
        $this->assign('province_list',$province_list);
        $this->assign('id',I('id'));
        $this->assign('p',I('p'));
        $this->assign('is_user_type',I('is_user_type'));
        $this->assign('is_disabled',I('is_disabled'));
        $this->assign('search_content',I('search_content'));
        $this->assign('username',I('username'));
        $this->display();
    }

    //成为经销商
    public function become_dealer(){
        if(IS_POST){
            $id = I('id');
            //上传文件
            $picture = upload();
            if($picture){
                $data['store_picture'] = $picture['store_picture'];
                $data['QR_code'] = $picture['QR_code'];
            }
            $data['dealer_id'] = $id;
            $data['dealer_name'] = I('dealer_name');
            $data['telephone'] = I('telephone');
            $data['store_name'] = I('store_name');
            $data['store_area'] = I('store_area');
            $data['open_time'] = I('open_time');
            $data['province'] = I('province');
            $data['city'] = I('city');
            $data['county'] = I('county');
            $data['address'] = I('address');
            $data['desc'] = I('desc');
            $data['add_time'] = date('Y-m-d H:i:s');
            //开启事务，先修改user表，再添加electrician表
            M()->execute('begin');
            if(false === M('user')->where('id='.$id)->save(array('vip'=>1,'user_type'=>3))){
                M()->execute('rollback');
                $this->error('操作失败');
            }
            $addResult = M('dealer')->add($data);
            if(!$addResult){
                M()->execute('rollback');
                $this->error('操作失败');
            }

            M()->execute('commit');
            $this->success('操作成功',U('user_list',array('p'=>I('p'),'is_user_type'=>I('is_user_type'),'is_disabled'=>I('is_disabled'),'search_content'=>I('search_content'))));
        }

        //获取省级列表
        $province = M('region')->where('pid=1')->select();

        //获取筛选和分页参数，分配变量
        $this->assign('province',$province);
        $this->assign('id',I('id'));
        $this->assign('p',I('p'));
        $this->assign('is_user_type',I('is_user_type'));
        $this->assign('is_disabled',I('is_disabled'));
        $this->assign('search_content',I('search_content'));
        $this->assign('username',I('username'));
        $this->display();
    }

    //删除
    public function user_del(){
        $id = I('id');
        $user_type = I('user_type');
        $p = I('p');
        $is_user_type = I('is_user_type');
        $search_content = I('search_content');
        $is_disabled = I('is_disabled');
        if(M('user')->where('id='.$id)->delete()){
            //如果是电工删除电工列表里信息，若为经销商删除经销商的列表里的信息
            if($user_type == 2){
                if(M('electrician')->where(array('electrician_id'=>$id))->delete() === false){
                    $this->error('数据异常');
                }
            }

            if($user_type == 3){
                if(M('dealer')->where(array('dealer_id'=>$id))->delete() === false){
                    $this->error('数据异常');
                }
            }

            $this->success('删除成功',U('user_list',array('p'=>$p,'is_user_type'=>$is_user_type,'search_content'=>$search_content)));
        }else{
            $this->error('数据异常',U('user_list',array('p'=>$p,'is_user_type'=>$is_user_type,'is_disabled'=>$is_disabled,'search_content'=>$search_content)));
        }
    }

    //地区联动
    public function region(){
        $where = array(
            'pid'=>I('pid')
        );
        $list = M('region')->where($where)->select();
        echo json_encode($list);
    }
}